package com.service.infoEntry.controller;

import com.service.config.utils.JsonXMLUtils;
import com.service.config.utils.RedisUtil;
import com.service.config.utils.Result;
import com.service.config.utils.UserTokenManager;
import com.service.infoEntry.dto.RouteDto;
import com.service.infoEntry.dto.UserDto;
import com.service.infoEntry.entity.CityInfo;
import com.service.infoEntry.entity.RouteInfo;
import com.service.infoEntry.service.RouteInfoService;
import com.service.infoEntry.task.ScheduleService;
import com.service.infoEntry.vo.RouteVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

import static com.service.infoEntry.constant.Constant.*;

/**
 * 行程信息表(RouteInfo)表控制层
 *
 * @author makejava
 * @since 2021-09-24 16:50:52
 */
@CrossOrigin
@RestController
@RequestMapping("routeInfo")
@Api(value = "行程相关接口")
public class RouteInfoController {
    /**
     * 服务对象
     */
    @Resource
    private RouteInfoService routeInfoService;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    ScheduleService scheduleService;
    /**
     * 通过主键查询单条数据
     * @param
     * @return
     */
    @RequestMapping(value =  "/selectOne1",method = RequestMethod.GET)
    @ApiOperation(value = "项目事务测试")
    @ResponseBody
    public Result selectOne1() throws Exception {
        scheduleService.getProgrammeInfoAndTransactionInfo();
        return Result.ok();
    }
    /**
     * 通过主键查询单条数据
     * @param
     * @return
     */
    @RequestMapping(value =  "/selectOne2",method = RequestMethod.GET)
    @ApiOperation(value = "行程单测试")
    @ResponseBody
    public Result selectOne2() throws Exception {
         scheduleService.getRouteInfo();
        return Result.ok();
    }
    /**
     * 通过主键查询单条数据
     * @param
     * @return
     */
    @RequestMapping(value =  "/selectOne",method = RequestMethod.GET)
    @ApiOperation(value = "通过主键查询单条数据")
    @ResponseBody
    public Result selectOne(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                     @RequestParam("routeId") String routeId) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                RouteInfo routeInfo = this.routeInfoService.queryById(routeId);
                return Result.ok().setData(routeInfo);
            } else {
                return Result.failure(USERNAME_OFF_SITE, LOGIN_INFO_HAS_EXPIRED);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    /**
     * 批量修改行程
     * @param cloudAuthToken
     * @param routeDtos
     * @return
     * @throws Exception
     */
    @RequestMapping(value =  "/updateRoute",method = RequestMethod.POST)
    @ApiOperation(value = "批量修改行程")
    @ResponseBody
    public Result updateRoute(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                           @RequestBody List<RouteDto> routeDtos) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);
                this.routeInfoService.update(routeDtos,userDto);
                return Result.ok();
            } else {
                return Result.failure(USERNAME_OFF_SITE, LOGIN_INFO_HAS_EXPIRED);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    /**
     * 删除行程
     * @param
     * @return
     */
    @RequestMapping(value =  "/deleteRoute",method = RequestMethod.DELETE)
    @ApiOperation(value = "删除行程")
    @ResponseBody
    public Result deleteRoute(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                              @RequestParam("routeId") String routeId) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                this.routeInfoService.deleteById(routeId);
                return Result.ok();
            } else {
                return Result.failure(USERNAME_OFF_SITE, LOGIN_INFO_HAS_EXPIRED);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    /**
     * 查询当前团队所有用户行程
     * @param cloudAuthToken token
     * @param teamId 团队id
     * @param date 起始日期
     * @param status 传递为1时，则只查询当前一周有出差的团队下的用户
     * @param username 作为模糊查询的用户名
     * @return
     * @throws ParseException
     */
    @RequestMapping(value =  "/queryRouteOfTeamUserByLimit",method = RequestMethod.GET)
    @ApiOperation(value = "查询当前团队所有用户行程")
    @ResponseBody
    public Result queryRouteOfTeamUser(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                       @RequestParam("teamId") String teamId,
                                       @RequestParam("date")String date,
                                       @RequestParam(value = "status",required = false)Integer status,
                                       @RequestParam(value = "username",required = false)String username) throws ParseException {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                List<RouteVo> routeVos = this.routeInfoService.queryAllRouteOfTeamUser(teamId, userId, date, username,status);
                return Result.ok().setData(routeVos);
            } else {
                return Result.failure(USERNAME_OFF_SITE, LOGIN_INFO_HAS_EXPIRED);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    /**
     * 查询指定用户行程
     * @param
     * @return
     */
    @RequestMapping(value =  "/queryRouteOfUser",method = RequestMethod.GET)
    @ApiOperation(value = "查询指定用户行程")
    @ResponseBody
    public Result queryRouteOfUser(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                       @RequestParam("date")String startDate
                                   ) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);

                RouteVo routeVo = this.routeInfoService.queryRouteByUser(userDto,startDate);
                return Result.ok().setData(routeVo);
            } else {
                return Result.failure(USERNAME_OFF_SITE, LOGIN_INFO_HAS_EXPIRED);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    /**
     * 添加用户填写的城市
     * @param
     * @return
     */
    @RequestMapping(value =  "/addCity",method = RequestMethod.GET)
    @ApiOperation(value = "添加用户填写的城市")
    @ResponseBody
    public Result addCity(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                   @RequestParam("city")String cityName) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);

                this.routeInfoService.addCity(userDto,cityName);
                return Result.ok();
            } else {
                return Result.failure(USERNAME_OFF_SITE, LOGIN_INFO_HAS_EXPIRED);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    /**
     * 添加用户填写的城市
     * @param
     * @return
     */
    @RequestMapping(value =  "/queryCity",method = RequestMethod.GET)
    @ApiOperation(value = "查询用户最近填入的5个城市")
    @ResponseBody
    public Result queryCity(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);

                Map<String,List<String>> cityInfos = this.routeInfoService.queryCity(userId);
                return Result.ok().setData(cityInfos);
            } else {
                return Result.failure(USERNAME_OFF_SITE, LOGIN_INFO_HAS_EXPIRED);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    /**
     * 修改输入城市的时间
     * @param
     * @return
     */
    @RequestMapping(value =  "/updateCity",method = RequestMethod.GET)
    @ApiOperation(value = "修改输入城市的时间")
    @ResponseBody
    public Result updateCity(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                             @RequestParam("city")String cityName) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);

                this.routeInfoService.updateCity(userId,cityName);
                return Result.ok();
            } else {
                return Result.failure(USERNAME_OFF_SITE, LOGIN_INFO_HAS_EXPIRED);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
}
